package dao

import (
	"advertising/internal/dal"
	"advertising/internal/models"
	"context"
	"strings"
	"time"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type MaterialAuditLog struct{}

func (MaterialAuditLog) Create(ctx context.Context, db *gorm.DB, MaterialID, MerchantID, MerchantName, AuditMerchantID, AuditMerchantName, AuditUser, AuditUsername string, AuditStatus int) (err error) {
	m := dal.Use(db).MaterialAuditLog
	var AuditData models.MaterialAuditLog
	AuditData.ID, _ = helper.UUID{}.String()
	AuditData.MaterialID = MaterialID
	AuditData.MerchantID = MerchantID
	AuditData.MerchantName = MerchantName
	AuditData.AuditMerchantID = AuditMerchantID
	AuditData.AuditMerchantName = AuditMerchantName
	AuditData.AuditUser = AuditUser
	AuditData.AuditUsername = AuditUsername
	AuditData.AuditStatus = AuditStatus
	AuditData.AuditAt = time.Now().Unix()
	m.WithContext(ctx).Create(&AuditData)
	return nil
}

func (MaterialAuditLog) List(ctx context.Context, db *gorm.DB, materialID string, page, limit int) (res []*models.MaterialAuditLog, count int64, err error) {
	m := dal.Use(db).MaterialAuditLog
	tx := m.WithContext(ctx)
	if materialID != "" {
		if strings.Contains(materialID, ",") {
			tx = tx.Where(m.MaterialID.In(strings.Split(materialID, ",")...))
		} else {
			tx = tx.Where(m.MaterialID.Eq(materialID))
		}
	}
	res, count, err = tx.Order(m.AuditAt.Desc()).FindByPage(Offset(page, limit))
	return
}
